#!/bin/bash

# release_freshness_report.sh v2

# Script that extracts release freshness information about Apertium language
# pairs and creates a CVS spreadsheet with this info
#
# It requires a file containing the list of released language pairs (together
# with their release dates). It also expects a directory containing the released
# language pairs (either extracted, or in .tar.gz archive, in which case the
# archives will be extracted) and a directory containing folders from the trunk.
# The spreadsheet will contain the version, date, and number of words in each
# dictionary and rules file for both the released language pair and the one in
# trunk.
#
# Created by Kamila Souckova <kamila@vesmir.sk> for the GCI 2010 contest

SRC_DIR="./trunk/"
RELEASED_DIR="./released/"
RELEASES_FILE="./releases.txt"

WORKING_DIR=`pwd`
# extract releases from .tar.gz archives if necessary
if [ -n "`ls $RELEASED_DIR/apertium-*.tar.gz`" ]; then
	cd $RELEASED_DIR
	for FILE in `ls apertium-*.tar.gz`; do
		echo -n "Extracting file $FILE..."
		tar -xzf $FILE && echo " done"
	done
	cd $WORKING_DIR
fi

# get a list of the releases we will be working with
REL_LIST="`cat $RELEASES_FILE | cut -f1 | grep 'apertium-*-*'`"

# here comes the interesting part: find the info and build the spreadsheet
REPORT_FILE="report-`date +%y-%m-%d`.csv"
echo "language_pair,version,date,dict_l1,dict_l2,dict_l1_l2,rules_l1_l2,\
rules_l2_l1,svn_lastchanged,dict_l1,dict_l2,dict_l1_l2,rules_l1_l2,\
rules_l2_l1" > $REPORT_FILE

for RELEASE in $REL_LIST; do
	# here not every release contains all these files, hence the 2>/dev/null
	echo -n " * Processing $RELEASE..."
	LANG1="`echo $RELEASE | cut -d- -f2`"
	LANG2="`echo $RELEASE | cut -d- -f3`"
	echo -n "$RELEASE," >> $REPORT_FILE # language_pair
	if [ -z `find $RELEASED_DIR -name $RELEASE-* -type d` ]; then
		echo " WARNING: Release for $RELEASE not found inside $RELEASED_DIR!"
		echo -n "??,??,??,??,??,??,??," >> $REPORT_FILE
	else
		echo -n "`find $RELEASED_DIR -name $RELEASE-* -type d | cut -d- -f4`," >> $REPORT_FILE # version
		echo -n "`cat $RELEASES_FILE | grep $RELEASE | cut -f2`," >> $REPORT_FILE # date
		echo -n "`cat $RELEASED_DIR/$RELEASE-*/$RELEASE.$LANG1.dix 2>/dev/null | grep '<e lm' | wc -l`," >> $REPORT_FILE # dict_l1
		echo -n "`cat $RELEASED_DIR/$RELEASE-*/$RELEASE.$LANG2.dix 2>/dev/null | grep '<e lm' | wc -l`," >> $REPORT_FILE # dict_l2
		echo -n "`cat $RELEASED_DIR/$RELEASE-*/$RELEASE.$LANG1-$LANG2.dix 2>/dev/null | grep '<l' | wc -l`," >> $REPORT_FILE # dict_l1_l2
		echo -n "`cat $RELEASED_DIR/$RELEASE-*/$RELEASE.$LANG1-$LANG2.t*x 2>/dev/null | grep '<rul' | wc -l`," 2>/dev/null >> $REPORT_FILE # rules_l1_l2
		echo -n "`cat $RELEASED_DIR/$RELEASE-*/$RELEASE.$LANG2-$LANG1.t*x 2>/dev/null | grep '<rul' | wc -l`," 2>/dev/null >> $REPORT_FILE # rules_l2_l1
	fi
	if [ ! -d "$SRC_DIR/$RELEASE/" ]; then
		echo " WARNING: Source for $RELEASE not found inside $SRC_DIR!"
		echo "??,??,??,??,??,??" >> $REPORT_FILE
	else
		echo -n "`svn info $SRC_DIR/$RELEASE/ | grep "Last Changed Date" | cut -d' ' -f4`," >> $REPORT_FILE; # svn_lastchanged
		echo -n "`cat $SRC_DIR/$RELEASE/$RELEASE.$LANG1.dix 2>/dev/null | grep '<e lm' | wc -l`," >> $REPORT_FILE # dict_l1
		echo -n "`cat $SRC_DIR/$RELEASE/$RELEASE.$LANG2.dix 2>/dev/null | grep '<e lm' | wc -l`," >> $REPORT_FILE # dict_l2
		echo -n "`cat $SRC_DIR/$RELEASE/$RELEASE.$LANG1-$LANG2.dix 2>/dev/null | grep '<l' | wc -l`," >> $REPORT_FILE # dict_l1_l2
		echo -n "`cat $SRC_DIR/$RELEASE/$RELEASE.$LANG1-$LANG2.t*x 2>/dev/null | grep '<rul' | wc -l`," >> $REPORT_FILE # rules_l1_l2
		echo  "`cat $SRC_DIR/$RELEASE/$RELEASE.$LANG2-$LANG1.t*x 2>/dev/null | grep '<rul' | wc -l`" >> $REPORT_FILE # rules_l2_l1
		echo " done"
	fi
done

echo "Report saved to $REPORT_FILE."

exit 0
